/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectBidScoreFeedBackController
 * 评标细则反馈表
 *
 * 修改纪录
 * 2022-05-24 版本：1.0 hudeyong 创建。
 * @version 版本：1.0
 * @author 作者：hudeyong
 * 创建日期：2022-05-24
 */

package com.topscomm.mpm.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.app.pojo.AppPageEntity;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.cbo.service.ICboAttachmentService;
import com.topscomm.cbo.service.impl.CboAttachmentService;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.controller.auto.MpmProjectBidScoreFeedBackControllerAuto;
import com.topscomm.mpm.pojo.MpmProjectBidScoreEntity;
import com.topscomm.mpm.pojo.MpmProjectBidScoreFeedBackEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.impl.MpmProjectBidScoreService;
import com.topscomm.mpm.service.impl.MpmProjectSaleService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;

/**
 * @author: hudeyong
 * @date: 2022-05-24
 * @description:评标细则反馈表Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectBidScoreFeedBack")
public class MpmProjectBidScoreFeedBackController extends MpmProjectBidScoreFeedBackControllerAuto implements IMpmProjectBidScoreFeedBackController {
	@Autowired
	private MpmProjectSaleService mpmProjectSaleService;
	@Autowired
	private MpmProjectService mpmProjectService;
	@Autowired
	private MpmProjectBidScoreService mpmProjectBidScoreService;
	/**
	 * @author: hudeyong
	 * @date: 2022-05-24
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
		{
			String dataPession = service.getDataPermission(TapOperationTypeEnum.Select, "", this.getUserBean());
			if (StringUtil.isEmpty(dataPession))// 默认权限只能看本人，部门负责人为自己的部门和下级部门的数据
			{
				String deptcodes_header = ConvertUtil.convertToString(this.getUserBean().getUser().getValue("deptcodes_header"));
				if (StringUtil.isEmpty(deptcodes_header))
					sql.appendSingle(" and (usercode = '{0}' or deptcode ='{1}' or deptcode = '{2}' )", this.getUserBean().getUser().getCode(), this.getUserBean().getDept().getCode(), this
							.getUserBean().getWorkDept().getCode());
				else
					sql.appendSingle(" and (deptcode in ({0})  or usercode = '{1}' or deptcode ='{2}' or deptcode = '{3}' )", deptcodes_header, this.getUserBean().getUser().getCode(), this
							.getUserBean().getDept().getCode(), this.getUserBean().getWorkDept().getCode());
			} else// 使用数据权限
			{
				sql.appendSingle(" and {0} ", dataPession);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: hudeyong
	 * @date: 2022-05-24
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: hudeyong
	 * @date: 2022-05-24
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交评标细则反馈表申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}

	/**
	 * @author: hudeyong
	 * @date: 2022-04-24
	 * @param paraMap
	 * @return
	 * @description:获取评分信息
	 * @modify:
	 */
	@GetMapping(value = "/queryScoreFeedBackApprovalInfo")
	public ResponseResult<Object> queryScoreFeedBackApprovalInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resultMap = new HashMap<>();
			String id = ConvertUtil.convertToString(paraMap.get("id"));
			MpmProjectBidScoreFeedBackEntity scoreFeedBackEntity = this.service.queryById(id);
			StringBufferProxy sql = new StringBufferProxy();
			String sourceid = ConvertUtil.convertToString(scoreFeedBackEntity.getSourceid());
			MpmProjectBidScoreEntity mpmProjectBidScoreEntity = mpmProjectBidScoreService.queryById(sourceid);
			sql.appendSingle("projectcode = '{0}'", scoreFeedBackEntity.getProjectcode());
			List<MpmProjectEntity> mpmProjectEntities = mpmProjectService.queryByWhere(sql.toString());
			sql.clear();
			sql.appendSingle("sourceid = '{0}' and id != '{1}' and state = '9' order by id",
					mpmProjectBidScoreEntity.getId(),sourceid);
			List<MpmProjectBidScoreFeedBackEntity> mpmProjectBidScoreFeedBackEntities = this.service.queryByWhere(sql.toString());
			if(mpmProjectEntities.size()>0) {
				resultMap.put("projectinfo", mpmProjectEntities.get(0));
				resultMap.put("scoreinfo", mpmProjectBidScoreEntity);
				resultMap.put("feedbackinfo", scoreFeedBackEntity);
				resultMap.put("feedbackList", mpmProjectBidScoreFeedBackEntities);
			} else {
				throw new Exception("未找到项目信息，请联系管理员");
			}

			return ResponseResult.ok(resultMap);
		} catch(Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/**
	 * @author: hudeyong
	 * @date: 2022-04-24
	 * @param paraMap
	 * @return
	 * @description:获取评分历史信息
	 * @modify:
	 */
	@GetMapping(value = "/queryScoreFeedBackHisInfo")
	public ResponseResult<Object> queryScoreFeedBackHisInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resultMap = new HashMap<>();
			StringBufferProxy sql = new StringBufferProxy();
			String sourceid = ConvertUtil.convertToString(paraMap.get("sourceid"));
			sql.appendSingle("sourceid = '{0}' and state = '9' order by id desc",
					sourceid);
			List<MpmProjectBidScoreFeedBackEntity> mpmProjectBidScoreFeedBackEntities = this.service.queryByWhere(sql.toString());
			resultMap.put("feedbackList", mpmProjectBidScoreFeedBackEntities);
			return ResponseResult.ok(resultMap);
		} catch(Exception ex) {
			return this.handleControllerException(ex);
		}
	}

	/**
	 * 审批详情(单表)
	 *
	 * @param paraMap
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	@SneakyThrows
	@GetMapping(value = "/loadMobilePageInfo")
	public ResponseResult<Object> loadMobilePageInfo(@RequestParam(required = false) Map<String, Object> paraMap) throws SQLException, IOException {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		String pageCode = this.service.getDao().getTableName();
		Map<String, Object> myDadaMap = new HashMap<>();
		//查询页面配置的sql语句
		StringBufferProxy sqlForAppPage = new StringBufferProxy();
		StringBufferProxy sqlForAppPageTab = new StringBufferProxy();
		sqlForAppPage.appendSingle("code='{0}' and enabled=1", pageCode);
		//获取页面信息
		AppPageEntity page = appPageService.queryObjectByWhere(sqlForAppPage.toString());
		//获取tab信息
		sqlForAppPageTab.appendSingle("pageid='{0}' and enabled=1", page.getId());
		List<Map<String, Object>> appPageTabList = appPageTabService.queryMapByWhere(sqlForAppPageTab.toString());
		// 获取sourceid对应的记录
		Map<String, Object> mainListData = this.service.queryMapById(paraMap.get("id").toString());
		StringBufferProxy sql = new StringBufferProxy();
		String sourceid = ConvertUtil.convertToString(mainListData.get("sourceid"));
		sql.appendSingle("id = '{0}'", sourceid);
		List<Map<String, Object>> bidScoreList = mpmProjectBidScoreService.queryMapByWhere(sql.toString());
		mpmProjectBidScoreService.convertClobToStringBidScore(bidScoreList);
		List<Map<String, Object>> mainList = new ArrayList<>();
		mainList.add(mainListData);
		myDadaMap.put("mainList", mainList);
		myDadaMap.put("MpmProjectBidScore", bidScoreList);
		//获取页面（约定：app管理页面配置中的页面编码对应controller表名）
		appPageService.getPageInfo(this.service.getDao().getTableName(), myDadaMap);
		return ResponseResult.ok(myDadaMap);
	}
}

